package com.jet.core.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.jet.core.exception.CustomException;
import com.jet.core.interceptor.ExcelReadListener;
import com.jet.core.model.vo.ExcelNoModelReadVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Slf4j
public class ExcelUtil<T> {

    public static void export(HttpServletResponse response, String fileName, String sheetName, Collection<?> data, Class clazz) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String name = URLEncoder.encode(fileName, "utf-8");
            response.setHeader("Content-disposition", String.format("attachment;filename=%s.xlsx", name));
            EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
        } catch (IOException e) {
            // 导出数据异常
            throw new CustomException("导出数据异常");
        }
    }

    public static void noModelExport(HttpServletResponse response, String fileName, String sheetName,
                                     List<List<String>> heads, Collection<?> data) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String name = URLEncoder.encode(fileName, "utf-8");
            response.setHeader("Content-disposition", String.format("attachment;filename=%s.xlsx", name));
            EasyExcel.write(response.getOutputStream()).head(heads).sheet(sheetName).doWrite(data);
        } catch (IOException e) {
            // 导出数据异常
            throw new CustomException("导出数据异常");
        }
    }

    public static <T> List<T> read(MultipartFile file, Class clazz) {
        ExcelReadListener<T> listener = new ExcelReadListener<>();
        try {
            EasyExcel.read(file.getInputStream(), clazz, listener).sheet(0).doRead();
            return listener.getDataList();
        } catch (IOException e) {
            throw new CustomException("导入数据异常");
        }
    }

    /**
     * 列标题不固定情况下excel
     *
     * @param file
     */
    public static ExcelNoModelReadVo noModelRead(MultipartFile file, int rowNumber) {
        try {
            ExcelReadListener<Map<Integer, String>> listener = new ExcelReadListener<>();
            EasyExcelFactory.read(file.getInputStream()).registerReadListener(listener).headRowNumber(rowNumber).sheet(0).doRead();
            List<Map<Integer, String>> headList = listener.getHeadList();
            List<Map<Integer, String>> dataList = listener.getDataList();
            if (CollectionUtils.isEmpty(headList)) {
                throw new CustomException("表头信息为空");
            }
            if (CollectionUtils.isEmpty(dataList)) {
                throw new CustomException("文件数据为空");
            }
            ExcelNoModelReadVo vo = new ExcelNoModelReadVo();
            vo.setHeadList(headList);
            vo.setDataList(dataList);
            return vo;
        } catch (IOException e) {
            throw new CustomException("导入数据异常");
        }
    }
}
